<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/9 10:53
 * Remark:
 */

namespace App\Http\Middleware\Auth;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ClientConsts\ClientConsts;
use App\Consts\RequestConsts\RequestMethodConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Auth\RoleModel;
use App\Models\Client\ClientModel;
use App\Models\Company\CompanyModel;
use App\Models\CreditProject\CreditProjectModel;
use Closure;
use Illuminate\Http\Exceptions\HttpResponseException;

class RoleLegitimacyMiddleware
{
    private CompanyModel $_companyModel;
    private RoleModel $_roleModel;

    public function __construct()
    {
        $this->_companyModel = new CompanyModel();
        $this->_roleModel = new RoleModel();
    }

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(\Illuminate\Http\Request $request, Closure $next): mixed
    {
        $method = strtolower($request->method());
        switch ($method){
            case RequestMethodConsts::GET_METHOD:
                //
                break;
            case RequestMethodConsts::POST_METHOD:
                $companyId = $request['company_id'] ?? TokenFuc::getCurrentStaffInfo()['company_id'];
                // 校验公司是否存在
                $this->verifyCompanyIsExists($request['company_id']);
                // 校验当前公司下角色是否存在
                $this->verifyCompanyRoleNameIsExists($request['company_id'],$request['role_name']);
               // 校验两个打分规则是否违规
                break;
            case RequestMethodConsts::PUT_METHOD:
                $roleId = $request->role_id;
                $this->verifyRoleIsExists($roleId);
                $roleInfo = $this->_roleModel->getRoleInfo($roleId,["company_id","role_name"]);
                if($request['role_name']){
                    $checkNames = $this->_roleModel->setGetEditCheckerNames("role_name",$request['role_name']);
                    $checkIds = $this->_roleModel->setGetEditCheckerIds("role_id",$roleId);
                    $conditions = [
                        $this->_roleModel->setGetEditCheckerCondition("company_id",$roleInfo['company_id'])
                    ];
                    $isExit = $this->_roleModel->editNameChecker($checkNames,$checkIds,$conditions);
                    if($isExit){
                        throw new HttpResponseException( ResponseFunc::returnJson(
                            'error',
                            null,
                            "当前名称已重复",
                            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                        ));
                    }
                }
                break;
            case RequestMethodConsts::PATCH_METHOD:
                $this->verifyRoleIsExists($request->role_id);
                break;
            default:
                break;
        }

        return $next($request);
    }

    /**
     * 校验公司名称是否存在
     * @param $companyName
     */
    private function verifyCompanyIsExists($companyId){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前公司不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,

            ));
        }
    }

    /**
     * 校验角色名称是否存在
     * @param $companyName
     */
    private function verifyCompanyRoleNameIsExists($companyId,$roleName){
        $isExist = $this->_roleModel->isExistByCondition([
            'company_id' => $companyId,
            'role_name' => $roleName,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前角色已存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * @param $clientId
     * @param string $msg
     */
    private function verifyRoleIsExists($roleId){
        $isExist = $this->_roleModel->isExistByCondition([
            'role_id' => $roleId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前角色不存在或已删除",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }



}